package com.joven.security.controller;

import com.alibaba.fastjson.JSON;
import com.joven.security.model.SysUser;
import com.joven.security.service.UserService;
import com.joven.security.utils.ResultData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Author wzj
 * @Date 2023-03-01 15:46
 **/
@Slf4j
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @PostMapping("/user")
    public ResultData login(@RequestBody SysUser user) {
        log.info("user login:{}", JSON.toJSONString(user));
        return userService.login(user);
    }

    @GetMapping("/selectByUserName")
    @PreAuthorize("hasAuthority('sys:user:add')")
    public ResultData selectByUserName(String userName) {
        log.info("userName:{}", userName);
        return userService.queryByUserName(userName);
    }

    @GetMapping("/out")
    @PreAuthorize("hasAuthority('sys:user:logout')")
    public ResultData logOut() {
        ResultData resultData = userService.logOut();
        return resultData;
    }
}
